﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemyFSM : CommonFSM
{
    protected BASEUNIT_STATE m_PrevState = BASEUNIT_STATE.IDLE;

    protected virtual void SetState(BASEUNIT_STATE iStateKey)
    {
    }

   //------------------------------State Change---------------------------------

    //CoverState
    protected virtual void CoverEnable()
    {
        if (gameObject.GetComponent<UnitStatus>().m_unitData.m_bCoverd == true)
        {
            gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.COVER;
            m_PrevState = gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState;
        }
    }

    protected virtual void CloseAtkEnable()
    {
        if (m_ObjCloseAtkTarget != null)
        {
            List<int> SurrTile = Calculate.GetAllSurroundingTile(m_iCurrentTileIdx);

            for (int i = 0; i < SurrTile.Count; ++i)            
            {
                if (m_ObjCloseAtkTarget.tag == "Player")
                {
                    if (SurrTile[i] == m_ObjCloseAtkTarget.GetComponent<CommonFSM>().m_iCurrentTileIdx)
                    {
                        m_PrevState = gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState;
                        gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.CLOSEATTACK;
                        break;
                    }
                }
                else if (m_ObjCloseAtkTarget.tag == "Cover")
                {
                    if (SurrTile[i] == m_ObjCloseAtkTarget.GetComponent<Build>().m_iListTileIdx[0])
                    {
                        m_PrevState = gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState;
                        gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.CLOSEATTACK;
                        break;
                    }
                }

                //if (SurrTile[i] == m_ObjCloseAtkTarget.GetComponent<CommonFSM>().m_iCurrentTileIdx) 0724 수정전
                //{
                //    m_PrevState = gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState;
                //    gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.CLOSEATTACK;
                //    break;
                //}
            }


        }
    }

    protected virtual void TraceEnable()
    {
        if (gameObject.GetComponent<UnitStatus>().m_WeaponData.m_WeaponType == WEAPON_TYPE.CLOSE)
        {
            if (EnemyTarget_Find("Player"))
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.TRACE;
        }

    }

    protected virtual void AimEnable()
    {
        if (EnemyTarget_Find("Player"))
        {
            gameObject.GetComponent<UnitStatus>().m_unitData.fCurrentDetect += Time.deltaTime;

            if(gameObject.GetComponent<UnitStatus>().m_unitData.iDetect <= gameObject.GetComponent<UnitStatus>().m_unitData.fCurrentDetect)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.fCurrentDetect = 0.0f;
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.AIM;
            }
        }
            
    }

    protected virtual void ThrowEnable()
    {
        if (gameObject.GetComponent<UnitStatus>().m_WeaponData.m_iCurrentAmmo <= 0)
            return;

        if (EnemyTarget_Find("Player"))
            gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.THROW;
    }

    protected virtual bool TauntEnable()
    {
        if (m_bTauntEnbale)
        {
            if (EnemyTarget_Find("Player"))
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.TAUNT;
                m_bTauntEnbale = false;
            }

            return true;
        }

        return false;
    }

    protected virtual bool ReloadEnalbe()
    {

        if (gameObject.GetComponent<UnitStatus>().m_unitData.iHavMagazine <= 0)
            return false;

        if (gameObject.GetComponent<UnitStatus>().m_WeaponData.m_iCurrentAmmo <= 0
        && gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState != BASEUNIT_STATE.CLOSEATTACK
            && gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState != BASEUNIT_STATE.DAMAGE)
        {
            gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.RELOAD;
            return true;
        }

        return false;
    }

    protected virtual bool CommonStateChange()
    {

        if (gameObject.GetComponent<UnitStatus>().m_unitData.iCurrentHP <= 0)
        {
            gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.DEAD;
            return true;
        }
        else
        {
            if (gameObject.GetComponent<UnitStatus>().m_unitData.iCurrentMorale <= 0)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.RETREAT;
                return true;
            }
        }

        if (m_ObjCloseAtkTarget != null && m_ObjCloseAtkTarget.tag == "Player")
            if (m_ObjCloseAtkTarget.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DEAD)
                m_ObjCloseAtkTarget = null;

        if (m_ObjTarget != null)
            if (m_ObjTarget.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DEAD)
                m_ObjTarget = null;

        return false;
    }

    protected void CommonCalculate()
    {
        //BlockTile Make
        List<GameObject> ListTile = GameObject.Find("DebugTile").GetComponent<Background>().m_ListTile;


        if (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState != BASEUNIT_STATE.MOVE)
        {
            int iTileidx = Calculate.GetMyTileIdx(transform.position);
            ListTile[m_iPrevTIleIdx = iTileidx].GetComponent<Tile>().m_iOption = 1;
            m_iCurrentTileIdx = iTileidx;
        }
    }

    void OnTriggerStay2D(Collider2D CollObj)
    {
        if (CollObj.gameObject.tag == "Player")
            if (CollObj.gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState != BASEUNIT_STATE.DEAD)
                m_ObjCloseAtkTarget = CollObj.gameObject;

        if (CollObj.gameObject.tag == "Cover")
            if (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.IDLE)
                m_ObjCloseAtkTarget = CollObj.gameObject;


    }

    void OnTriggerExit2D(Collider2D CollObj)
    {
        if (CollObj.gameObject.tag == "Player")
            m_ObjCloseAtkTarget = null;
    }

}
